Command Design Pattern

struct Command{
virtual void call() const=0;
}; // 커맨드 인터페이스
// 명령을 바로 수행하는 것이 아닌 Command에 대한 클래스에 저장
struct BankAccountCommand: Command{
BankAccount& account; // 커맨드 작업 대상
enum Action{ deposit, withdraw } action; // 작업 종류
int amount;
BankAccountCommand(BankAccount& account, const Action action, const int amount): account(account), action(action), amount(amount) {}
};
void call() const override {
switch(action){
case deposit:
account.deposit(amount);
break;
case withdraw:
account.withdraw(amount);
break;
}
}
// 사용
BankAccount ba;
Command cmd{ba, BankAccountCommand::deposit, 100};
cmd.call();